package org.niux.AutoLoginQQ.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.cookie.Cookie;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.niux.AutoLoginQQ.common.Constant;

/**
 * 
 * Copyright (c) 2009,niux<br>
 * All rights reserved.<br>
 * 
 * 文件名称：FileOperate.java<br>
 * 摘    要：文件读写工具类<br>
 * <br>
 * 当前版本：<br>
 * 作    者：mingxian Email: <a href="mailto:liangmingxian@gmail.com">liangmingxian@gmail.com</a><br>
 * 完成日期：@since 2012-9-13<br>
 * <br>
 * 取代版本：<br>
 * 原作者  ：<br>
 * 完成日期：@since Oct 22, 2009<br>
 */
public class FileOperate {
	
	/**
	 * 
	 * 方法概述：导出QQ列表文件
	 * 描述：导出QQ列表文件
	 * @param file
	 * @param qqs
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2012-9-14
	 */
	public void write(File file, List<String> qqs){
		StringBuffer sb = new StringBuffer();
		
		for (String exportString : qqs) {
			sb.append(exportString);
		}
		
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			bw.write(sb.toString());
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 方法概述：写入cookies数据
	 * 描述：写入cookies数据
	 * @param file
	 * @param data
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2012-9-14
	 */
	public void writeCookies(File file,Map<String, List<Cookie>> data){
		StringBuffer sb = new StringBuffer();
		
		for (String key : data.keySet()) {
			List<Cookie> cookies = data.get(key);
			
			StringBuffer cookiesString = new StringBuffer();
			
			for (Cookie cookie : cookies) {
				cookiesString.append(cookie.getName() + "=" + cookie.getValue() + ";");
			}
			
			cookiesString.substring(0, cookiesString.length() - 1);
			
			sb.append(key + Constant.SPLIT_STR + cookiesString + "\r\n");
		}
		
		try {
			BufferedWriter bw = new BufferedWriter(new FileWriter(file));
			bw.write(sb.toString());
			bw.flush();
			bw.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 方法概述：读取cookies数据
	 * 描述：读取cookies数据
	 * @param file
	 * 返回类型：void
	 * 修改人：梁铭显
	 * 修改时间：2012-9-14
	 */
	public void readCookies(File file){
		BufferedReader reader = null;
		
		try {
			reader = new BufferedReader(new FileReader(file));
			
			String tempString;
			while((tempString = reader.readLine()) != null) {
				if(tempString.equals("")){
					continue;
				}
				
				List<Cookie> cookies = new ArrayList<Cookie>();
				
				String qqNum = tempString.split(Constant.SPLIT_STR)[0];
				String cookiesString = tempString.split(Constant.SPLIT_STR)[1];
				
				String[] cookieArray = cookiesString.split(";");
				
				for (String string : cookieArray) {
					
					String name = "";
					String value = "";
					
					if(string.split("\\=").length == 1){
						name = string.split("\\=")[0];
						value = "";
					} else {
						name = string.split("\\=")[0];
						value = string.split("\\=")[1];
					}
					
					BasicClientCookie cookie = new BasicClientCookie(
							name, value);
					
					if(name.equals("confirmuin")){
						cookie.setDomain("ptlogin2.qq.com");
						cookie.setPath("/");
					} else {
						cookie.setDomain("qq.com");
						cookie.setPath("/");
					}
					
					cookies.add(cookie);
				}
				
				Constant.LOGIN_DATA.put(qqNum, cookies);
			}
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
